package org.example.SolutionNew005;

import java.util.ArrayList;
import java.util.List;

public class Backtrack003 {
    public static void main(String[] args) {
        Backtrack003 b=new Backtrack003();
        String digits="23";
        System.out.println(b.letterCombinations(digits).toString());

    }

    public enum Number{
        Two_List('2',"abc"),
        Three_List('3',"def"),
        Four_List('4',"ghi"),
        Five_List('5',"jkl"),
        Six_List('6',"mno"),
        Seven_List('7',"pqrs"),
        Eight_List('8',"tuv"),
        Nine_List('9',"wxyz");

        private final char number;
        private final String letters;

        Number(char number,String leffters){
            this.number=number;
            this.letters=leffters;
        }

        public char getNumber(){return number;}
        public String getLetters(){return letters;}
    }

    public Number getNumber(char number){
        for(Number n:Number.values()){
            if(n.getNumber()==number){
                return n;
            }
        }
        return null;
    }

    List<String> res=new ArrayList<>();
    public List<String> letterCombinations(String digits) {
        if(digits==null || digits.length()==0){return res;}
        char[] chars=digits.toCharArray();
        StringBuilder builder=new StringBuilder();
        letterCombination(chars,builder,0);
        return res;
    }

    public void letterCombination(char[] chars,StringBuilder builder,int index){
        if(index==chars.length){
            res.add(new String(builder));
            return;
        }
        char[] letters=getNumber(chars[index]).getLetters().toCharArray();
        for(char letter:letters){
            builder.append(letter);
            letterCombination(chars,builder,index+1);
            builder.deleteCharAt(builder.length()-1);
        }
    }
}
